#!/bin/bash
#if [ "$(id -u)" != "0" ]; then
#   echo "This script must be run as root" 1>&2
#   exit 1
#fi

prog=`basename $0`;

export INCOMING_DIR=/mnt/CBSCentral/Incoming
export OUTGOING_DIR=/mnt/CBSCentral/PreArchive
export LOG_DIR=/XNAT/Logs
export XNAT_INSTALL=CBS
export AE_TITLE=CBS
export DCM2XNAT_PROG=/home/vmuser/perl-xnat/dcm2xnat.storescp

export DCMDICTPATH=/usr/share/dcmtk/dicom.dic
export DCMDATAPATH=$INCOMING_DIR

export DIR_PREFIX=${AE_TITLE}
export LOG_STAMP=`date +'%Y.%m.%d-%H.%M'`;
export STORESCP_LOG="${LOG_DIR}/${LOG_STAMP}-$prog-$$.log"
export STORESCP_PID_FILE="${LOG_DIR}/dicomreceiver.pid"

if [ ! -e "${INCOMING_DIR}" ]; then
    echo "ERROR: No such directory '${INCOMING_DIR}'!";
    exit -1;
fi

if [ ! -e "${OUTGOING_DIR}" ]; then
    echo "ERROR: No such directory '${OUTGOING_DIR}'!";
    exit -1;
fi

if [ ! -e "${LOG_DIR}" ]; then
    echo -n "Creating '${LOG_DIR}':";
    if mkdir -p "${LOG_DIR}"; then
	echo " OK"
    else
	echo " ERROR!"
	exit -1;
    fi
fi

#echo "Checking for existing pid file '${STORESCP_PID_FILE}'"

if [ -e "${STORESCP_PID_FILE}" ]; then
    pid=`cat "${STORESCP_PID_FILE}"`;
    #exists=`ps -p $pid -o cmd= | grep storescp 2>/dev/null`
    exists=`ps -p $pid -o comm= | grep storescp 2>/dev/null`
    if [ -z "${exists}" ]; then
	echo "Previous storescp process (pid=$pid) is no longer running. Removing PID file '${STORESCP_PID_FILE}'";
	if rm -vf "${STORESCP_PID_FILE}"; then
	    echo " - Removed"
	else
	    echo " - ERROR! Aborting"
	    exit 1;
	fi
    else
	echo "Previous storescp process (pid=$pid) is STILL running. Kill and remove pid file before re-starting.";
	echo " - Try:"
	echo "   kill $pid && rm -f '${STORESCP_PID_FILE}'";
	exit 1;
    fi
fi

exists=`ps -C storescp -o pid=`
if [ -n "${exists}" ]; then
	echo "WARNING: storescp process is running but there's no '${STORESCP_PID_FILE}' file!";
	echo "Previous storescp process is running kill before re-starting!";
	echo " - Try:"
	echo "   kill $exists";
	exit 1;
fi
# --promiscuous needed for all those odd SOPs
# --verbose and --debug for help
cmd="storescp --promiscuous --filename-extension '.dcm' --output-directory '${INCOMING_DIR}' --exec-on-eostudy '${DCM2XNAT_PROG} #p' --aetitle '${AE_TITLE}' --sort-conc-studies '${DIR_PREFIX}' --eostudy-timeout 30 4444 >> '${STORESCP_LOG}' 2>> '${STORESCP_LOG}' &"
#cmd="storescp --promiscuous --filename-extension '.dcm' --output-directory '${INCOMING_DIR}' --aetitle '${AE_TITLE}' --sort-conc-studies '${DIR_PREFIX}' --eostudy-timeout 3 4444 >> '${STORESCP_LOG}' 2>> '${STORESCP_LOG}' &"
#cmd="storescp -v --filename-extension '.dcm' +ac -aet '${AE_TITLE}' -od '${INCOMING_DIR}' --exec-on-eostudy '${DCM2XNAT_PROG} #p' -ss '${DIR_PREFIX}' 4444 >> '${STORESCP_LOG}' 2>> '${STORESCP_LOG}' &"

#echo "# Going to run:"
#echo $cmd

echo -n "Starting storescp (lgo: '${STORESCP_LOG}')"

echo $cmd >> "${STORESCP_LOG}"
eval $cmd >> "${STORESCP_LOG}"
dicomreceiver_pid=$!

echo "${dicomreceiver_pid}" > "${STORESCP_PID_FILE}"

echo -n " with pid=$dicomreceiver_pid .. double checking .."

sleep 1;

exists=`ps -p $dicomreceiver_pid -o comm= | grep storescp 2>/dev/null`
if [ -n "${exists}" ]; then
    echo " OK!";
else
    echo " ERROR!";
    exit 1;
fi

echo "Run the following for more information:"
echo "   cat ${STORESCP_LOG}";
